package net.htjs.juc.util;

import java.util.Map;

public class UserContextHolder2 {

    private final ThreadLocal<Map<String,String>> threadLocal;

    private UserContextHolder2() {
        this.threadLocal = new ThreadLocal<>();
    }

    /**
     * 静态内部类单例
     * 单例初始化
     */
    private static class SingletonHolder {
        private static final UserContextHolder2 INSTANCE = new UserContextHolder2();
    }

    public static UserContextHolder2 getInstance() {
        return SingletonHolder.INSTANCE;
    }

    /**
     * 设置上下文
     * @param userContext
     */
    public void set(Map<String,String> userContext) {
        threadLocal.set(userContext);
    }

    /**
     * 获取上下文
     * @return
     */
    public Map<String,String> get() {
        return threadLocal.get();
    }

    /**
     * 移除上下文
     */
    public void remove() {
        threadLocal.remove();
    }
}
